package com.example.actionsignin.mvp.view;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.actionsignin.R;
import com.example.actionsignin.entity.DescribeEntity;
import com.example.actionsignin.entity.ImagesEntity;
import com.example.actionsignin.entity.UserEntity;
import com.example.actionsignin.http.HttpUtil;
import com.example.actionsignin.utils.ToastUtil;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 活动管理详情
 */
public class ActionManagerActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = "ActionManagerActivity";
    private String mAid = "";
    private String mName = "";

    private TextView mJoin;
    private TextView mSign;
    private TextView mSign_no;
    private TextView mJoin_Now;
    private TextView mSign_Now;

    private int mType = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_manager);

        mAid = getIntent().getStringExtra("aid");
        if (mAid == null || mAid.equals(""))
        {
            finish();
        }

        initView();
        getData();
    }

    private void initView() {
        findViewById(R.id.activity_action_manager_left).setOnClickListener(this);
        findViewById(R.id.activity_action_manager_delete).setOnClickListener(this);
        findViewById(R.id.activity_action_manager_join).setOnClickListener(this);
        findViewById(R.id.activity_action_manager_sign).setOnClickListener(this);

        mJoin = (TextView) findViewById(R.id.activity_action_join_all);
        mSign = (TextView) findViewById(R.id.activity_action_sign_all);
        mSign_no = (TextView) findViewById(R.id.activity_action_sign_no);
        mJoin_Now = (TextView) findViewById(R.id.activity_action_join_now);
        mSign_Now = (TextView) findViewById(R.id.activity_action_sign_now);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.activity_action_manager_left:
                finish();
                break;
            case R.id.activity_action_manager_delete:
                deleteData();
                break;
            case R.id.activity_action_manager_join:
                Intent intent = new Intent(ActionManagerActivity.this,JoinDescribeActivity.class);
                intent.putExtra("aid",mAid);
                startActivity(intent);
                break;
            case R.id.activity_action_manager_sign:
                Intent intent1 = new Intent(ActionManagerActivity.this,SignDescribeActivity.class);
                intent1.putExtra("aid",mAid);
                startActivity(intent1);
                break;
        }
    }

    private void deleteData() {
        HttpUtil.onSend().onDeleteAction(mAid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull String loginEntity) {
                        ToastUtil.INSTANCE(getApplicationContext(),"删除成功");
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d(TAG, "onError: " + e);
                    }

                    @Override
                    public void onComplete() {
                        Intent intent = new Intent(ActionManagerActivity.this,ManagerActivity.class);
                        intent.putExtra("type","delete");
                        setResult(RESULT_OK,intent);
                        finish();
                    }
                });
    }

    private void getData()
    {
        HttpUtil.onSend().onActionDescribe(mAid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<DescribeEntity<UserEntity,ImagesEntity>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull DescribeEntity<UserEntity,ImagesEntity> userEntityDescribeEntity) {
                        String join_all = userEntityDescribeEntity.getSize();
                        String sign_all = String.valueOf(userEntityDescribeEntity.getSigns().size());
                        String sign_no = String.valueOf(Integer.valueOf(userEntityDescribeEntity.getSize()) - userEntityDescribeEntity.getSigns().size());
                        mType = Integer.valueOf(userEntityDescribeEntity.getType());
                        mJoin.setText(join_all);
                        mSign.setText(sign_all);
                        mSign_no.setText(sign_no);
                        mJoin_Now.setText(join_all);
                        mSign_Now.setText(sign_all);
                        mName = userEntityDescribeEntity.getName();
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d(TAG, "onError: " + e);
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }
}
